/**
 * 培训计划列表
 */
<template>
	<vp-main-page @refresh="loadDataList" :tableRef="tableRef">
		<template #search>
			<el-form ref="searchFormRef" :model="searchForm" @submit.prevent @keyup.enter="loadDataList" label-width="120px">
				<vp-search-row :show-more="showMore">
					<vp-search-col>
						<el-form-item prop="nd">
							<template #label><vp-label zh="年度" en="Year"/></template>
							<el-date-picker
								v-model="searchForm.nd"
								type="year"
								value-format="YYYY"
								clearable
							/>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="kc">
							<template #label><vp-label zh="课程" en="Course"/></template>
							<el-input v-model="searchForm.kc" v-trim clearable></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="pxbh">
							<template #label><vp-label zh="培训编号" en="Training No." /></template>
							<el-input v-model="searchForm.pxbh" v-trim clearable></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="lxList">
							<template #label><vp-label zh="计划类型" en="Plan Type" /></template>
							<el-select v-model="searchForm.lxList" multiple collapse-tags clearable>
								<el-option :value="10" label="年度计划"></el-option>
                            	<el-option :value="20" label="临时"></el-option>
							</el-select>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="ztList">
							<template #label><vp-label zh="状态" en="Status"/></template>
							<VpStatusMultSelect 
								v-model="searchForm.ztList" 
								:statusDesc="statusMap" 
								:optional="[10, 20, 30, 98, 99]" 
							/>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="fsbsList">
							<template #label><vp-label zh="是否复训" en="Retraining" /></template>
							<el-select v-model="searchForm.fsbsList" multiple collapse-tags clearable>
								<el-option :value="10" label="初训"></el-option>
                            	<el-option :value="20" label="复训"></el-option>
							</el-select>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="jhKsrq">
							<template #label><vp-label zh="计划日期" en="Plan Date"/></template>
							<el-date-picker
								v-model="searchForm.jhKsrq"
								type="daterange"
								value-format="YYYY-MM-DD"
								unlink-panels
								range-separator="-"
								class="w100"
								clearable
							>
							</el-date-picker>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="fbt">
							<template #label><vp-label zh="副标题" en="Subtitle"/></template>
							<el-input v-model="searchForm.fbt" v-trim clearable></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col btn-col>
						<vp-btn-search :loading="loading" @click="loadDataList"/>
						<vp-btn-reset @click="handleSearchReset"/>
						<vp-btn-more @change="showMore = $event" />
					</vp-search-col>
				</vp-search-row>
			</el-form>
		</template>
		<template #actions>
			<vp-btn-add v-auth="'trainingPlan.edit'" @click="handleAdd"/>
		</template>
		<template #table>
			<vp-vxe-table
				id="trainingPlanTable"
				ref="tableRef" 
				:data="tableData" 
				@sort-change="handleSortChange" 
				row-key="id"
				v-loading="loading"
			>	
				<vxe-column type="seq" align="center" title="#" width="50" />
				<vxe-column 
					sortable
					field="jh_ksrq" 
					title="计划日期 Plan Date"
					show-overflow
					width="190"
				>
					<template #header><vp-label zh="计划日期" en="Plan Date" /></template>
					<template #default="{ row }">
						<span>{{ row.jhKsrq }}</span>
						<span v-if="row.jhJsrq"> ~ </span>
						<span>{{ row.jhJsrq }}</span>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="kcbh" 
					title="课程编号 Course No."
					show-overflow
					width="120"
				>
					<template #header><vp-label zh="课程编号" en="Course No."/></template>
					<template #default="{ row }">
						<el-link @click="handleViewCourse(row.kcid)">{{ row.kcbh }}</el-link>
					</template>
				</vxe-column>
				<vxe-column 
					field="kcmc" 
					title="课程名称 Course Name"
					show-overflow 
					width="200"
				>
					<template #header><vp-label zh="课程名称" en="Course Name" /></template>
				</vxe-column>
				<vxe-column 
					field="fsbs" 
					title="是否复训 Retraining"
					show-overflow 
					width="90"
				>
					<template #header><vp-label zh="是否复训" en="Retraining" /></template>
					<template #default="{ row }">
						<span>{{ row.fsbs === 10 ? '初训' : row.fsbs === 20 ? '复训' : '' }}</span>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="pxbh" 
					title="培训编号 Training No."
					show-overflow 
					width="120"
				>
					<template #header><vp-label zh="培训编号" en="Training No."/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="lx" 
					title="计划类型 Plan Type"
					show-overflow 
					width="100"
				>
					<template #header><vp-label zh="计划类型" en="Plan Type"/></template>
					<template #default="{ row }">
						<span>{{ row.lx === 10 ? '年度计划' : row.fsbs === 20 ? '临时' : '' }}</span>
					</template>
				</vxe-column>
				<vxe-column 
					field="cxry" 
					title="参训人员 Participants"
					show-overflow 
					width="160"
				>
					<template #header><vp-label zh="参训人员" en="Participants"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="zt" 
					title="状态 Status" 
					show-overflow
					width="120"
				>
					<template #header><vp-label zh="状态" en="Status" /></template>
					<template #default="{ row }">
						{{ getStatusDesc(row.zt, statusMap) }}
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="sjKsrq" 
					title="实际培训日期 Actual Date"
					show-overflow 
					width="130"
				>
					<template #header><vp-label zh="实际培训日期" en="Actual Date"/></template>
				</vxe-column>
				<vxe-column 
					field="operation" 
					title="操作 Operation"
					fixed="right" 
					width="280"
				>
					<template #header><vp-label-operations/></template>
					<template #default="scope">
						<el-space>
							<vp-btn-edit v-if="scope.row.zt === 10" text v-auth="'trainingPlan.edit'" @click="handleEdit(scope.row)"/>
							<vp-btn v-if="scope.row.zt === 20" type="primary" text v-auth="'trainingPlan.edit'" @click="handleIssue(scope.row)">
								<vp-label zh="下发" en="Issue"/>
							</vp-btn>
							<vp-btn v-if="(scope.row.zt === 30 || scope.row.zt === 99) && !scope.row.mydwjid" type="primary" text v-auth="'trainingPlan.edit'" @click="handleIssueTeacherEval(scope.row)">
								<vp-label zh="下发教员评分" en="Issue Trainer Eval."/>
							</vp-btn>
							<vp-btn-edit v-if="scope.row.zt === 30" text v-auth="'trainingPlan.edit'" @click="handleEditResult(scope.row)"/>
							<vp-btn-close v-if="scope.row.zt === 30" text v-auth="'trainingPlan.edit'" @click="handleClose(scope.row)"/>
							<vp-btn-del v-if="scope.row.zt === 10" text v-auth="'trainingPlan.edit'" @click="handleDelete(scope.row)"/>
							<vp-btn-discard v-if="scope.row.zt === 20 || scope.row.zt === 30" text v-auth="'trainingPlan.edit'" @click="handleDiscard(scope.row)"/>
							<vp-btn v-if="scope.row.mydwjid" text  @click="handleViewTrainingTeacherEval(scope.row.mydwjid)">
								<vp-label zh="查看教员评分" en="View Trainer Eval."/>
							</vp-btn>
							<vp-btn-view text @click="handleView(scope.row.id)"/>
						</el-space>
					</template>
				</vxe-column>
			</vp-vxe-table>
		</template>
		<template #pagination>
			<vp-pagination
				@change="loadDataList"
				:total="page.total" 
				v-model:currentPage="page.pageNum"
				v-model:pageSize="page.pageSize"
			>
			</vp-pagination>
		</template>
		<EditTrainingPlan ref="editTrainingPlanRef" @formSubmited="loadDataList"></EditTrainingPlan>
		<IssueTrainingPlan ref="issueTrainingPlanRef" @formSubmited="loadDataList"></IssueTrainingPlan>
		<CloseTrainingPlan ref="closeTrainingPlanRef" @formSubmited="loadDataList"></CloseTrainingPlan>
		<EditTrainingPlanReult ref="editTrainingPlanReultRef" @formSubmited="loadDataList"></EditTrainingPlanReult>
	</vp-main-page>
</template>
<script setup lang="ts" name="trainingPlan">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue'
import request from '/@/utils/request';
import { useRouter } from 'vue-router';
import { DEFAULT_PAGE } from '/@/utils/page';
import VpMessageBox from '/@/components/vp/VpMessageBox';
import commonFunction from '/@/utils/commonFunction';
import { formatDate } from '/@/utils/formatTime';
import VpStatusMultSelect from "/@/components/vp/status/VpStatusMultSelect.vue";

const EditTrainingPlan = defineAsyncComponent(() => import('./component/EditTrainingPlan.vue'));
const IssueTrainingPlan = defineAsyncComponent(() => import('./component/IssueTrainingPlan.vue'));
const CloseTrainingPlan = defineAsyncComponent(() => import('./component/CloseTrainingPlan.vue'));
const EditTrainingPlanReult = defineAsyncComponent(() => import('./component/EditTrainingPlanReult.vue'));

const { getStatusDesc } = commonFunction();

const statusMap = {
	30 : {zh: '下发', en: 'Issue'},
};

const searchForm = reactive({
	kc: "",
	pxbh: "",
	fbt: "",
	lxList: [],
	fsbsList: [],
	jhKsrq: [],
	ztList: [10, 20, 30, 99],
	nd: formatDate(new Date(), 'YYYY'),
});

const page = reactive({
	...DEFAULT_PAGE,
})

const showMore = ref(false);
const loading = ref(false);
const tableData = ref([]);

const router = useRouter();

// 加载列表数据
const loadDataList = () => {
	loading.value = true;
	request.get('/training/plan/list', {
		params: {
			...searchForm,
			...page,
		}
	}).then(res => {
		tableData.value = res?.data?.list || [];
		page.total = res?.data?.total || 0;
		page.pageNum = res?.data?.pageNum || 1;
	}).finally(()=>{
		loading.value = false;
	})
}

const searchFormRef = ref();
// 重置搜索条件
const handleSearchReset = ()=>{
	searchFormRef.value.resetFields();
}

// 列表字段排序
const handleSortChange = (column: any)=>{
	page.sortColumn = column.field;
	page.sortOrder = column.order;
	loadDataList()
}

onMounted(() => {
	loadDataList();
})

const editTrainingPlanRef = ref();
const tableRef = ref();

// 新增
const handleAdd = ()=>{
	editTrainingPlanRef.value.handleOpen({
		type: 'add',
	});
}

// 修改
const handleEdit = (row: any)=>{
	editTrainingPlanRef.value.handleOpen({
		type: 'edit',
		id: row.id
	});
}

// 下发
const issueTrainingPlanRef = ref();
const handleIssue = (row: any)=>{
	issueTrainingPlanRef.value.handleOpen({
		type: 'issue',
		id: row.id
	});
}

// 修改培训结果
const editTrainingPlanReultRef = ref();
const handleEditResult = (row: any)=>{
	editTrainingPlanReultRef.value.handleOpen({
		type: 'editResult',
		id: row.id
	});
}

// 关闭
const closeTrainingPlanRef = ref();
const handleClose = (row: any)=>{
	closeTrainingPlanRef.value.handleOpen({
		type: 'close',
		id: row.id
	});
}

// 查看
const handleView = (id: string)=>{
	router.push({
		name: "viewTrainingPlan",
		params: {
			id: id,
		},
	})
}

// 查看课程
const handleViewCourse = (id: string)=>{
	router.push({
		name: "viewTrainingCourse",
		params: {
			id: id,
		},
	})
}

// 删除
const handleDelete = (row: any)=>{
	VpMessageBox.confirmDelete().then(()=>{
		loading.value = true;
		request.delete(`/training/plan/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

// 作废
const handleDiscard = (row: any)=>{
	VpMessageBox.confirmOperation().then(()=>{
		loading.value = true;
		request.post(`/training/plan/discard/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

// 下发教员评分
const handleIssueTeacherEval = (row: any) => {
	VpMessageBox.confirmOperation().then(()=>{
		loading.value = true;
		request.post(`/training/plan/teacherEval/issue/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

// 查看教员评分
const handleViewTrainingTeacherEval = (id: string) => {
	router.push({
		name: "viewTrainingTeacherEval",
		params: {
			id: id,
		},
	})
}

</script>

<style scoped lang="scss">
</style>